package com.chainyoung.component_tmperson.ui.activity.cloudwallet;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.entity.PurseAddr;
import com.chainyoung.component_tmperson.qrcodeview.QRCodeUtil;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletAddrContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.WalletAddrPresenter;
import com.chainyoung.component_tmperson.zxing.WriterException;
import com.chainyoung.router.RouterConstants;

import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.chainyoung.component_tmperson.constant.PersonStringConstant.COIN_CN_NAME;
import static com.chainyoung.component_tmperson.constant.PersonStringConstant.COIN_LOGO;
import static com.chainyoung.component_tmperson.constant.PersonStringConstant.COIN_NAME;
import static com.chainyoung.component_tmperson.constant.PersonStringConstant.COIN_TOTAL_NAME;


/**
 * 充值界面
 */

@Route(path = RouterConstants.PATH_CHARGE)
public class ChargeActivity extends BaseActivity<WalletAddrPresenter> implements WalletAddrContract.View {


    @BindView(R2.id.tv_purse_addr)
    TextView tvPurseAddr;
    @BindView(R2.id.copy)
    TextView copy;
    @BindView(R2.id.tv_bit_type)
    TextView tvBitType;
    @BindView(R2.id.iv_code)
    ImageView ivCode;

    private String coinName;
    private String coinCnName;

    //剪切板管理工具类
    private ClipboardManager mClipboardManager;
    //剪切板Data对象
    private ClipData mClipData;

    private String coinTotalName;

    private String logo;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        coinName = getIntent().getStringExtra(COIN_NAME);
        coinCnName = getIntent().getStringExtra(COIN_CN_NAME);
        coinTotalName = getIntent().getStringExtra(COIN_TOTAL_NAME);
        logo = getIntent().getStringExtra(COIN_LOGO);
        setCurrentTitle(String.format(getString(R.string.tmperson_recharge_title), coinName.toUpperCase() + "/" + coinCnName));
        tvBitType.setText(String.format(getString(R.string.wallet_tips), coinName.toUpperCase() + "/" + coinCnName));
        mClipboardManager = (ClipboardManager) mContext.getSystemService(mContext.CLIPBOARD_SERVICE);
    }

    @Override
    protected void initData() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("coin", coinName);
        mPresenter.getMyPurse(map);
    }

    private void getBitMap() {
        Glide.with(mContext)
                .load(logo)
                .apply(RequestOptions.centerCropTransform().override(400, 400))
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        BitmapDrawable bd = (BitmapDrawable) resource;
                        Bitmap bitmap = bd.getBitmap();
                        generateQrcode(bitmap);
                    }
                });

    }

    private void generateQrcode(Bitmap bitmap) {
        try {
            String codeText = coinTotalName.toLowerCase() + ":" + tvPurseAddr.getText().toString().trim();
            int width= ConvertUtils.dp2px(mContext,150);
            Bitmap bm = QRCodeUtil.createQRCode(codeText, bitmap,150,150);
            ivCode.setImageBitmap(bm);
            ivCode.setScaleType(ImageView.ScaleType.FIT_XY);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_charge;
    }


    @OnClick({R2.id.copy})
    void onClick(View view) {
        if (view.getId() == R.id.copy) {
            String text = tvPurseAddr.getText().toString();
            //创建一个新的文本clip对象
            mClipData = ClipData.newPlainText("block", text);
            //把clip对象放在剪贴板中
            mClipboardManager.setPrimaryClip(mClipData);
            ToastUtil.showToast(getString(R.string.tmperson_addr_copy_success));
        }
    }

    @Override
    public void onGetPurseAddrSuccess(PurseAddr data) {
        tvPurseAddr.setText(data.getQianbao());
        getBitMap();
    }

}
